package com.lc.luzhengtang.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.adapter.SearchStaticListAdapter;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.CommenBean;
import com.lc.luzhengtang.bean.SearchBean;
import com.lc.luzhengtang.conn.ConnAddShop;
import com.lc.luzhengtang.conn.ConnGoodsSearch;
import com.lc.luzhengtang.layoutmanager.FullyGridLayoutManager;
import com.lc.luzhengtang.util.LovelyToast;
import com.lc.luzhengtang.view.ClearEditText;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by Administrator on 2017/5/3.
 */

public class SearchListActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.img_news)
    ImageView imgNews;
    @BindView(R.id.text_price)
    TextView textPrice;
    @BindView(R.id.text_sort_order)
    TextView textSortOrder;
    @BindView(R.id.text_goods_comment)
    TextView textGoodsComment;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;

    List<SearchBean.DataBeanX.DataBean> dataBeanList;
    public int currentPage = 1;
    SearchStaticListAdapter mSearchListAdapter;
    public int type = 1;
    @BindView(R.id.et_search)
    ClearEditText etSearch;
    @BindView(R.id.layout_search)
    LinearLayout layoutSearch;
    String status = "1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_list);
        ButterKnife.bind(this);
        dataBeanList = new ArrayList<>();
        etSearch.setText(getIntent().getStringExtra("searchcode"));
        if (etSearch.getText().toString().isEmpty()) {
        /*    LovelyToast.makeText(SearchListActivity.this, "请输入要搜索的信息", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);*/

        } else {

            initData();
//        onRefresh2();
            etSearch.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                @Override
                public void afterTextChanged(Editable s) {
//                onRefresh();
                    if (etSearch.getText().toString().equals("")) {
                      /*  LovelyToast.makeText(SearchListActivity.this, "请输入要搜索的信息", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);*/
                        recyclerView.setVisibility(View.GONE);
                    } else {
                        recyclerView.setVisibility(View.VISIBLE);

                        initData();
                    }


                }
            });

        }


    }

    public void initData() {
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        //recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setLayoutManager(new FullyGridLayoutManager(SearchListActivity.this, 2));

        mSearchListAdapter = new SearchStaticListAdapter(null);
        mSearchListAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        mSearchListAdapter.isFirstOnly(false);
//        Drawable drawable = ContextCompat.getDrawable(SearchListActivity.this, R.drawable.line_divider);
//        recyclerView.addItemDecoration(new SimpleDividerItemDecoration(SearchListActivity.this, drawable, 1));

        recyclerView.setAdapter(mSearchListAdapter);

        refreshLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
        refreshLayout.setOnRefreshListener(this);
        mSearchListAdapter.setOnLoadMoreListener(this);

        //开启loading,获取数据
        setRefreshing(true);
        onRefresh();
        mSearchListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {


            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                new ConnAddShop(dataBeanList.get(position).getId() + "", BaseApplication.basePreferences.readUid(), "1", "盒", dataBeanList.get(position).getSales(), new AsyCallBack<CommenBean>() {

                    @Override
                    public void onStart(int type) throws Exception {
                        super.onStart(type);
                    }

                    @Override
                    public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                        super.onSuccess(toast, type, o, commenBean);
                        //  Toast.makeText(getActivity(), "成功加入购物车", Toast.LENGTH_SHORT).show();
                        LovelyToast.makeText(SearchListActivity.this, commenBean.getMessage(), 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {
                        super.onFail(toast, type);
                    }

                    @Override
                    public void onEnd(String toast, int type) throws Exception {
                        super.onEnd(toast, type);
                        //可能需要移除之前添加的布局

                    }
                }

                ).execute(SearchListActivity.this, false);
            }
        });
    }

    @Override
    public void onRefresh() {
        new ConnGoodsSearch(etSearch.getText().toString().trim(), status, BaseApplication.basePreferences.readUid(), 1, new AsyCallBack<SearchBean>() {
            @Override
            public void onSuccess(String toast, int type, Object o, SearchBean s) throws Exception {
                super.onSuccess(toast, type, o, s);
                dataBeanList = s.getData().getData();
                if (dataBeanList != null) {
                    currentPage = 1;
                    mSearchListAdapter.setNewData(dataBeanList);
                }

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                refreshLayout.setRefreshing(false);
                mSearchListAdapter.setEnableLoadMore(false);
            }
        }

        ).execute(SearchListActivity.this, false);


    }

    public void onRefresh2() {
        new ConnGoodsSearch(getIntent().getStringExtra("searchcode"), status, BaseApplication.basePreferences.readUid(), 1, new AsyCallBack<SearchBean>() {
            @Override
            public void onSuccess(String toast, int type, Object o, SearchBean s) throws Exception {
                super.onSuccess(toast, type, o, s);
                dataBeanList = s.getData().getData();
                if (dataBeanList != null) {
                    currentPage = 1;
                    mSearchListAdapter.setNewData(dataBeanList);
                }

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                refreshLayout.setRefreshing(false);
                mSearchListAdapter.setEnableLoadMore(false);
            }
        }

        ).execute(SearchListActivity.this, false);


    }

    @Override
    public void onLoadMoreRequested() {
        new ConnGoodsSearch(etSearch.getText().toString().trim(), status, BaseApplication.basePreferences.readUid(), 1, new AsyCallBack<SearchBean>() {
            @Override
            public void onSuccess(String toast, int type, Object o, SearchBean s) throws Exception {
                super.onSuccess(toast, type, o, s);
                dataBeanList = s.getData().getData();
                if (dataBeanList != null) {
                    currentPage = 1;
                    mSearchListAdapter.setNewData(dataBeanList);
                } else {
                    //显示没有更多数据
                    // newsAdapter.loadComplete();
                    mSearchListAdapter.loadMoreComplete();
                    View noDataView = LayoutInflater.from(SearchListActivity.this).inflate(R.layout.item_no_data, (ViewGroup) recyclerView.getParent(), false);
                    mSearchListAdapter.addFooterView(noDataView);
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                refreshLayout.setRefreshing(false);
                //dismissLoading();
                mSearchListAdapter.removeAllFooterView();
                //最后调用结束刷新的方法
                setRefreshing(false);
            }
        }

        ).execute(SearchListActivity.this, false);
    }

    public void setRefreshing(final boolean refreshing) {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(refreshing);
            }
        });
    }


    @OnClick({R.id.text_price, R.id.text_sort_order, R.id.text_goods_comment, R.id.img_news})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_news:
                finish();
                break;
            case R.id.text_price:
                status = "1";
                textPrice.setTextColor(getResources().getColor(R.color.classfiy_select));
                textSortOrder.setTextColor(getResources().getColor(R.color.classfiy_unselect));
                textGoodsComment.setTextColor(getResources().getColor(R.color.classfiy_unselect));
//                initData();
                if (etSearch.getText().toString().equals("")) {
                    recyclerView.setVisibility(View.GONE);
                } else {
                    recyclerView.setVisibility(View.VISIBLE);
                    initData();
                }
                break;
            case R.id.text_sort_order:
                status = "2";
                textPrice.setTextColor(getResources().getColor(R.color.classfiy_unselect));
                textSortOrder.setTextColor(getResources().getColor(R.color.classfiy_select));
                textGoodsComment.setTextColor(getResources().getColor(R.color.classfiy_unselect));
                if (etSearch.getText().toString().equals("")) {
                    recyclerView.setVisibility(View.GONE);
                } else {
                    recyclerView.setVisibility(View.VISIBLE);
                    initData();
                }

                break;
            case R.id.text_goods_comment:
                status = "3";
                textPrice.setTextColor(getResources().getColor(R.color.classfiy_unselect));
                textSortOrder.setTextColor(getResources().getColor(R.color.classfiy_unselect));
                textGoodsComment.setTextColor(getResources().getColor(R.color.classfiy_select));
//                initData();
                if (etSearch.getText().toString().equals("")) {
                    recyclerView.setVisibility(View.GONE);
                } else {
                    recyclerView.setVisibility(View.VISIBLE);
                    initData();
                }
                break;
        }
    }


    public void refreshListData() {


    }


}
